﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.IO;
//using IWshRuntimeLibrary;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace BugSnapper
{
    [RunInstaller(true)]
    public partial class BugSnapper : System.Configuration.Install.Installer
    {
        public BugSnapper()
        {
            InitializeComponent();
            this.Committing += new InstallEventHandler(BugSnapper_Committing);
            this.Committed += new InstallEventHandler(BugSnapper_Committed);
            this.AfterUninstall += new InstallEventHandler(BugSnapper_AfterUninstall);
            this.BeforeInstall += new InstallEventHandler(BugSnapper_BeforeInstall);
            this.AfterInstall += new InstallEventHandler(BugSnapper_AfterInstall);
        }

        void BugSnapper_AfterInstall(object sender, InstallEventArgs e)
        {

        }

        void BugSnapper_BeforeInstall(object sender, InstallEventArgs e)
        {

        }

        void BugSnapper_AfterUninstall(object sender, InstallEventArgs e)
        {

        }

        void BugSnapper_Committed(object sender, InstallEventArgs e)
        {
            try
            {
                this.InstallWMEncoder();
            }
            catch (Exception ex)
            {
                (new Logger()).ProcessError(ex);
            }
        }

        void BugSnapper_Committing(object sender, InstallEventArgs e)
        {

        }

        private void InstallWMEncoder()
        {
            try
            {
                DirectoryInfo dr = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                if (System.IO.File.Exists(dr.ToString() + "\\WMEncoder.msi"))
                {
                    // Prepare the process to run
                    ProcessStartInfo start = new ProcessStartInfo();
                    // Enter in the command line arguments, everything you would enter after the executable name itself
                    start.Arguments = " /i\"" + dr.ToString() + "\\WMEncoder.msi\" /t\"" + dr.ToString() + "\\temp\\\" /quiet";
                    // Enter the executable to run, including the complete path
                    start.FileName = "msiexec.exe";
                    // Do you want to show a console window?
                    //start.WindowStyle = ProcessWindowStyle.Hidden;
                    //start.CreateNoWindow = true;

                    // Prepare the process to run
                    ProcessStartInfo start1 = new ProcessStartInfo();
                    // Enter in the command line arguments, everything you would enter after the executable name itself
                    start1.Arguments = "/q:n";
                    // Enter the executable to run, including the complete path
                    start1.FileName = dr.ToString() + "\\SaveAsPDF.exe";
                    // Do you want to show a console window?
                    //start.WindowStyle = ProcessWindowStyle.Hidden;
                    //start.CreateNoWindow = true;
                                        
                    // Prepare the process to run
                    ProcessStartInfo start2 = new ProcessStartInfo();
                    // Enter in the command line arguments, everything you would enter after the executable name itself
                    start2.Arguments = "/p \"" + dr.ToString() + "\\WindowsMedia9-KB929182-INTL.msp\" " + "/qb REINSTALLMODE=\"ecmus\" REINSTALL=\"ALL\" /quiet";
                    // Enter the executable to run, including the complete path
                    start2.FileName = "msiexec.exe";
                    // Do you want to show a console window?
                    //start.WindowStyle = ProcessWindowStyle.Hidden;
                    //start.CreateNoWindow = true;

                    try
                    {
                        string patchFileCode = "@echo off"
                            + System.Environment.NewLine
                            + "msiexec.exe /i\"" + dr.ToString() + "\\WMEncoder.msi\" /t\"" + dr.ToString() + "\\temp\\\" /quiet"
                            + System.Environment.NewLine
                            + "cd\\"
                            + System.Environment.NewLine
                            + "cd " + dr.ToString()
                            + System.Environment.NewLine
                            + "SaveAsPDF.exe /q:n"
                            + System.Environment.NewLine
                            + "msiexec.exe /p \"" + dr.ToString() + "\\WindowsMedia9-KB929182-INTL.msp\" " + "/qb REINSTALLMODE=\"ecmus\" REINSTALL=\"ALL\" /quiet"
                            + System.Environment.NewLine
                            + "exit";
                        System.IO.File.WriteAllText(dr.ToString() + "\\patch.bat", patchFileCode);
                    }
                    catch (Exception ex)
                    {
                        (new Logger()).ProcessError(ex);
                    }
                    // Run the external process & wait for it to finish
                    using (Process proc = Process.Start(start))
                    {
                        proc.WaitForExit();
                        while (!proc.HasExited)
                        {
                        }
                        using (Process proc1 = Process.Start(start1))
                        {
                            proc1.WaitForExit();
                            while (!proc1.HasExited)
                            {
                            }
                            using (Process proc2 = Process.Start(start2))
                            {
                                proc.WaitForExit();
                                while (!proc2.HasExited)
                                {
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                (new Logger()).ProcessError(ex);
            }
        }

    }


    public static class Wow
    {
        public static bool Is64BitProcess
        {
            get { return IntPtr.Size == 8; }
        }

        public static bool Is64BitOperatingSystem
        {
            get
            {
                // Clearly if this is a 64-bit process we must be on a 64-bit OS.
                if (Is64BitProcess)
                    return true;
                // Ok, so we are a 32-bit process, but is the OS 64-bit?
                // If we are running under Wow64 than the OS is 64-bit.
                bool isWow64;
                return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
            }
        }

        static bool ModuleContainsFunction(string moduleName, string methodName)
        {
            IntPtr hModule = GetModuleHandle(moduleName);
            if (hModule != IntPtr.Zero)
                return GetProcAddress(hModule, methodName) != IntPtr.Zero;
            return false;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        extern static IntPtr GetCurrentProcess();
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        extern static IntPtr GetModuleHandle(string moduleName);
        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
    }
}
